package com.thomas.sharing.mybatis.dao;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.thomas.sharing.mybatis.model.Displacement;
import com.thomas.sharing.mybatis.model.PriceResult;
import com.thomas.sharing.mybatis.model.Product;
import com.thomas.sharing.mybatis.request.PriceRequest;

@Component
@Qualifier("carDaoJdbcTemplate")
public class CarDaoJdbcTemplate implements CarDao {
	
	private static final Log LOG = LogFactory.getLog(CarDaoJdbcTemplate.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Product> getProductsByBrands(List<String> brands) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Displacement> getDisplacementByBrandsAndProducts(List<String> brands, List<String> products) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PriceResult> getPrices(PriceRequest priceRequest) {
		List<String> brandList = priceRequest.getBrands();
		List<String> productList = priceRequest.getProducts();
		List<BigDecimal> displacementList = priceRequest.getDisplacements();
		StringBuilder sql = new StringBuilder();
		sql.append("		SELECT DISTINCT CAR.BRANDNAME, CAR.PRODUCT, CAR.MODEL, CAR.DISPLACEMENT, FOURS.NAME AS FOURSNAME, PRICE.PRICE " ) 
			.append(" FROM THOMASDB.CAR " ) 
			.append(" JOIN THOMASDB.PRICE ON CAR.ID = PRICE.CARID " ) 
			.append(" JOIN THOMASDB.FOURS ON FOURS.ID = PRICE.FOURSID ")
			.append( " where ");
		if(brandList != null && brandList.size() > 0){
			sql.append(" CAR.BRANDNAME in ");
			sql.append(" ( ");
			brandList.forEach(brand -> sql.append("'").append(brand).append("'").append(","));
			sql.deleteCharAt(sql.length() - 1);
			sql.append(")");
			
		}
		if(productList != null && productList.size() > 0){
			sql.append(" AND ");
			sql.append(" CAR.PRODUCT in ");
			sql.append(" ( ");
			productList.forEach(product -> sql.append("'").append(product).append("'").append(","));
			sql.deleteCharAt(sql.length() - 1);
			sql.append(")");
		}
		if(displacementList != null && displacementList.size() > 0){
			sql.append("    ");
			sql.append(" CAR.DISPLACEMENT in ");
			sql.append(" ( ");
			displacementList.forEach(displacement -> sql.append(displacement).append(","));
			sql.deleteCharAt(sql.length() - 1);
			sql.append(")");
		}
		sql.append(" ORDER BY PRICE ");
		LOG.info(sql);
		return this.jdbcTemplate.query(sql.toString(), new PriceMapper());		
	}
	
	class PriceMapper implements RowMapper<PriceResult>{
		public PriceResult mapRow(ResultSet rs, int rowNum) throws SQLException{
			PriceResult priceResult = new PriceResult();
			priceResult.setBrandName(rs.getString("BRANDNAME"));
			priceResult.setProduct(rs.getString("PRODUCT"));
			priceResult.setModel(rs.getString("MODEL"));
			priceResult.setDisplacement(rs.getBigDecimal("DISPLACEMENT"));
			priceResult.setFoursName(rs.getString("FOURSNAME"));
			priceResult.setPrice(rs.getInt("PRICE"));
			return priceResult;
			
		}
	}

}
